package com.taxi.driver.module.account.newpwd;

import android.content.Context;
import android.content.Intent;
import android.inputmethodservice.KeyboardView;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Selection;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianxx.utils.text.RegUtils;
import com.socks.library.KLog;
import com.taxi.driver.BuildConfig;
import com.taxi.driver.R;
import com.taxi.driver.common.BaseActivity;
import com.taxi.driver.config.IConstants;
import com.taxi.driver.event.UserEvent;
import com.taxi.driver.module.account.firstlogin.FirstLoginActivity;
import com.taxi.driver.module.account.newpwd.dagger.DaggerNewPwdComponent;
import com.taxi.driver.module.account.newpwd.dagger.NewPwdModule;
import com.taxi.driver.module.login.LoginActivity;
import com.taxi.driver.util.SpaceFilter;
import com.taxi.driver.widget.EditTextPlus;
import com.taxi.driver.widget.ImageViewPlus;
import com.taxi.driver.widget.SecKeyboardView;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 功能描述：输入新密码页
 */
public class NewPwdActivity extends BaseActivity implements NewPwdContract.View {

    /**
     * @param context
     * @param phone
     * @param isFirstLogin
     * @param idCard
     */
    public static void actionStart(Context context, String phone, boolean isFirstLogin, String idCard, String code) {
        Intent intent = new Intent(context, NewPwdActivity.class);
        intent.putExtra(IConstants.PHONE, phone);
        intent.putExtra(IConstants.ISFIRSTLOGIN, isFirstLogin);
        intent.putExtra(IConstants.IDCARD, idCard);
        intent.putExtra(IConstants.CODE, code);
        context.startActivity(intent);
    }


    @BindView(R.id.img_head_left)
    ImageView mImgHeadLeft;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.et_new_pwd)
    EditTextPlus mEtNewPwd;
    @BindView(R.id.iv_switch)
    ImageViewPlus mIvSwitch;
    @BindView(R.id.btn_submit)
    AppCompatButton mBtnSubmit;
    @BindView(R.id.keyboard_view)
    KeyboardView mKeyboardView;

    String mPhone;
    boolean mIsFirstLogin;
    String mIdCard;
    String mCode;

    @Inject
    NewPwdPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_pwd);
        ButterKnife.bind(this);
        DaggerNewPwdComponent.builder()
                .appComponent(getAppComponent())
                .newPwdModule(new NewPwdModule(this))
                .build()
                .inject(this);

        mPhone = getIntent().getStringExtra(IConstants.PHONE);
        mIsFirstLogin = getIntent().getBooleanExtra(IConstants.ISFIRSTLOGIN, false);
        mIdCard = getIntent().getStringExtra(IConstants.IDCARD);
        mCode = getIntent().getStringExtra(IConstants.CODE);
        if (mIsFirstLogin) {
            mImgHeadLeft.setVisibility(View.VISIBLE);
            mImgHeadLeft.setEnabled(true);
        } else {
            mImgHeadLeft.setVisibility(View.GONE);
            mImgHeadLeft.setEnabled(false);
        }
        mTvTitle.setText(mIsFirstLogin ? R.string.first_login : R.string.forget_pwd_title);
        mEtNewPwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                KLog.e(RegUtils.isStringFormatCorrectNew(mEtNewPwd.getText().toString()));
            }

            @Override
            public void afterTextChanged(Editable s) {

                    boolean isEnable = s.length() >= 8;
                    mBtnSubmit.setEnabled(isEnable);

            }
        });

        mEtNewPwd.setFilters(new InputFilter[]{new SpaceFilter(this)});
        //更改hint字体大小

            SpannableString hintString = new SpannableString("8~16位大小写字母、数字及符号");
            AbsoluteSizeSpan hintSize = new AbsoluteSizeSpan(16, true);
            hintString.setSpan(hintSize, 0, hintString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mEtNewPwd.setHint(hintString);


        mImgHeadLeft.setOnClickListener(v -> {
            finish(); //关闭界面
        });
    }

    @OnClick({R.id.iv_switch, R.id.btn_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_switch:
                boolean showPwd = !mIvSwitch.isSelected();
                mIvSwitch.setSelected(showPwd);
                changPwdDisplay(showPwd);
                break;
            case R.id.btn_submit:
                mPresenter.resetPwd(mPhone, mCode, mIdCard, mEtNewPwd.getEditableText().toString().trim());
                break;
        }
    }

    public void changPwdDisplay(boolean showPwd) {
        if (showPwd) {
            mEtNewPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            mEtNewPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        Selection.setSelection(mEtNewPwd.getEditableText(), mEtNewPwd.getEditableText().length());
    }

    @Override
    public boolean isActive() {
        return false;
    }

    @Override
    public void resetSuccess(boolean hasTempUserInfo) {
        //发送消息，销毁其它页面
        EventBus.getDefault().post(new UserEvent(UserEvent.RESET_PWD_SUCCESS));
        if (mIsFirstLogin && hasTempUserInfo) {
            /* 首次登录，查看司机信息 */
            FirstLoginActivity.actionStart(this);
        } else {
            /* 直接跳转登录页 */
            LoginActivity.start(this);
        }
        finish(); //关闭界面
    }
}
